package com.xiaoma.tpo.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.dao.AdvertisementDao;
import com.xiaoma.tpo.domain.AdVisit;
import com.xiaoma.tpo.domain.ImieLoginUser;
import com.xiaoma.tpo.domain.PcVisitDevice;
import com.xiaoma.tpo.dto.AdCountVO;
import com.xiaoma.tpo.dto.AdVo;
import com.xiaoma.tpo.dto.PhyVersionVO;
import com.xiaoma.tpo.service.AdService;
import com.xiaoma.tpo.service.ImieLoginUserService;
import com.xiaoma.tpo.service.PcVisitDeviceService;

@Controller
public class OthersWS {

    @Autowired
    private PcVisitDeviceService service;
    
    @Autowired
    private AdvertisementDao advertisementDao;
    
    @Autowired
    private ImieLoginUserService imieLoginUserService;
    
    @Autowired
    private AdService adService;
    
    @RequestMapping(value = "/service/pc/visit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> visitDevice(@RequestBody PcVisitDevice deviceDto) {
        Map<String, Object> result = new HashMap<String,Object>();
            Date now = new Date();
            PcVisitDevice diviceNo = service.getByDeviceNo(deviceDto.getDeviceNo());
            if(diviceNo != null){
            	deviceDto.setCreatedAt(diviceNo.getCreatedAt());
            }else{
            	deviceDto.setCreatedAt(now);
            }
            deviceDto.setUpdatedAt(now);
            deviceDto.setCount(1);
            service.save(deviceDto);
            result.put("status", 1);
        return result;	
    }
    
    @RequestMapping(value = "/advertisement",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> advertisement() {
        Map<String, Object> result = new HashMap<String,Object>();
        result.put("advertisements", advertisementDao.findAll());
        return result;
    }
    
    @RequestMapping(value = "/test/migrate", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> migrate(String version){
        Map<String, Object> result = new HashMap<String, Object>();
        String fixedVersion = "301";
        if (version.equalsIgnoreCase("3.0") || (Integer.parseInt(fixedVersion) > Integer.parseInt(version))) {
            String md5Str = DigestUtils.md5Hex(fixedVersion);
            result.put("version", fixedVersion);
            result.put("md5", md5Str);
            result.put("needUpdate", true);
            result.put("desc", "update version " + fixedVersion);
        } else {
            result.put("needUpdate", false);
        }
        return result;
        
    }
    //手机端接口记录IMIE号第一次登陆时间和次数
    @RequestMapping(value="/remember/loginuser",method=RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> rememberLoginUser(@RequestParam String imie){
    	Map<String, Object> result = new HashMap<String,Object>();
    	ImieLoginUser  imieLoginUser = (ImieLoginUser)imieLoginUserService.handleImie(imie);
    	result.put("imie", imieLoginUser);
    	return result;
    }
    //获取广告信息
    @RequestMapping(value="/service/get/ad",method=RequestMethod.GET)
    @ResponseBody
    public List<AdVo> getAd(){
    	List<AdVo> adAds = adService.getAllAd();
    	return adAds;
    }
    //获取版本信息
    @RequestMapping(value="/service/get/version",method=RequestMethod.GET)
    @ResponseBody
    public List<PhyVersionVO> getVersion(@RequestParam String guid, String version){
    	List<PhyVersionVO> vVersions = null;
    	if(version != null && !"".equals(version)){
    		 vVersions = adService.getNewVersion(guid,version);
    	}else{
    		vVersions = adService.getVersion(guid);
    	}
    	return vVersions;
    }
    //广告点击率记录
    @RequestMapping(value="/service/add/ad/count",method={RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> addAdCount(@RequestBody AdVisit adVisit){
    	Map<String, Object> map = new HashMap<String, Object>();
    	Integer b = adService.addAdCount(adVisit);
    	map.put("Status", b);
    	return map;
    }
    
}
